package com.jxdinfo.hussar.cloud.module.example.dljc.basketdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jxdinfo.hussar.cloud.module.example.dljc.basketdata.dao.BasketDataMapper;
import com.jxdinfo.hussar.cloud.module.example.dljc.basketdata.model.BasketData;
import com.jxdinfo.hussar.cloud.module.example.dljc.basketdata.service.BasketDataService;
import com.jxdinfo.hussar.cloud.module.example.dljc.basketdata.vo.BasketDataPageVO;
import com.jxdinfo.hussar.cloud.module.example.dljc.basketdata.vo.BasketDataSearchVO;
import com.jxdinfo.hussar.platform.core.base.apiresult.ApiResponse;
import com.jxdinfo.hussar.support.datasource.annotations.HussarDs;
import com.jxdinfo.hussar.support.mp.base.service.impl.HussarServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 吊篮监测实时数据服务实现类
 */
@Service("dljc.basketdata.BasketDataServiceImpl")
@HussarDs("master")
public class BasketDataServiceImpl extends HussarServiceImpl<BasketDataMapper, BasketData> implements BasketDataService {

    private static final Logger logger = LoggerFactory.getLogger(BasketDataServiceImpl.class);

    private static final String RETURN_CODE = "0";

    @Autowired
    private BasketDataMapper basketDataMapper;

    @Override
    public ApiResponse<BasketDataPageVO> findByPage(BasketDataSearchVO basketDataSearchVO) {
        QueryWrapper<BasketData> wrapper = new QueryWrapper<>();

        Page<BasketData> page = new Page<>();
        page.setPages(basketDataSearchVO.getPage());
        page.setSize(basketDataSearchVO.getRows());

        if(ObjectUtils.isNotEmpty(basketDataSearchVO.getUnloaId())) {
            wrapper.eq("unloa_id", basketDataSearchVO.getUnloaId());
        }
        if(StringUtils.isNotEmpty(basketDataSearchVO.getUnloadName())) {
            wrapper.like("unload_name", basketDataSearchVO.getUnloadName());
        }
        if(ObjectUtils.isNotEmpty(basketDataSearchVO.getTyProjectCode())) {
            wrapper.eq("ty_project_code", basketDataSearchVO.getTyProjectCode());
        }

        wrapper.orderByDesc("collection_time");

        Page<BasketData> basketDataPage = basketDataMapper.selectPage(page, wrapper);
        BasketDataPageVO basketDataPageVO = new BasketDataPageVO();
        basketDataPageVO.setCode(RETURN_CODE);
        basketDataPageVO.setData(basketDataPage.getRecords());
        basketDataPageVO.setCount(basketDataPage.getTotal());

        return ApiResponse.success(basketDataPageVO);
    }
}
